package com.lin.springcloud.controller;

import com.lin.springcloud.pojo.Dept;
import com.lin.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ author:lin tingfeng
 * @ data:2021/10/9-16:35
 */
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept) {
        return deptService.addDept(dept);
    }


    @HystrixCommand(fallbackMethod = "hystrixGet")
    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable Long id) {
        Dept dept = deptService.queryDeptById(id);
        if (dept == null)
            throw new RuntimeException("不存在该用户");
            return dept;
    }
    //备选方法
    public Dept hystrixGet(@PathVariable Long id) {
        return new Dept()
                .setDeptno(id)
                .setDeptname("不存在该用户--Hystrix服务熔断备选方法")
                .setDb_source("No This DATABASE!");
    }

    @GetMapping("/dept/list")
    public List<Dept> getAll() {
        return deptService.queryDeptAll();
    }

}

